﻿'  __ _                 
' / _| |                
'| |_| |_   _ _ __ _ __ 
'|  _| | | | | '__| '__|
'| | | | |_| | |  | |   
'|_| |_|\__,_|_|  |_|

'Copyright (C) 2012, Rafal Marek Chmiel
'All rights reserved. http://flurr.codeplex.com/

'Copyright (C) 2010, Stephen Perelson (WinTumblr)
'All rights reserved. http://www.feedfeast.com/wintumblr/

' Redistribution and use in source and binary forms, with or without modification, are 
' permitted provided that the following conditions are met:
'
' - Redistributions of source code must retain the above copyright notice, this list 
'   of conditions and the following disclaimer.
' - Redistributions in binary form must reproduce the above copyright notice, this list 
'   of conditions and the following disclaimer in the documentation and/or other 
'   materials provided with the distribution.
' - Neither the name of The Flurr Project (both Flurr and Flurr Client) nor the names 
'   of its contributors may be 
'   used to endorse or promote products derived from this software without specific 
'   prior written permission.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
' ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
' WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
' IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
' INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
' NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
' PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
' ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
' POSSIBILITY OF SUCH DAMAGE.

Public Class textForm

    Private tumblrData As Flurr.tumblrPostText
    Private acct As New Flurr.tumblrAccount

    Public Sub New(ByVal account As Flurr.tumblrAccount)
        InitializeComponent()
        acct = account
    End Sub

    Private Sub createButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles createButton.Click
        'createButton.Text = "Please Wait..."
        'createButton.Enabled = False
        'cancelButton.Enabled = False

        tumblrData = New Flurr.tumblrPostText
        Dim t As New Flurr.tumblrTextPost

        With t
            .email = acct.email
            .password = acct.password
            .title = titleTextBox.Text
            .body = postTextBox.Text
            .tags = tagsTextBox.Text
            .isPrivate = privatePostCheckBox.Checked
        End With

        Dim ap As New Flurr.tumblrPostText.aPostText(AddressOf tumblrData.postText)

        Dim result As IAsyncResult = ap.BeginInvoke(t, New AsyncCallback(AddressOf CallbackMethod), ap)
    End Sub

    Private Sub CallbackMethod(ByVal ar As IAsyncResult)
        If Me.InvokeRequired Then
            Me.BeginInvoke(New AsyncCallback(AddressOf CallbackMethod), ar)
            Exit Sub
        End If

        Dim caller As Flurr.tumblrPostText.aPostText = DirectCast(ar.AsyncState, Flurr.tumblrPostText.aPostText)
        Dim returnValue As Flurr.tumblrStatus = caller.EndInvoke(ar)

        If returnValue.code <> 201 Then
            MsgBox(returnValue.message)
        Else
            MsgBox("Post Created")
            Me.Close()
            titleTextBox.Clear()
            postTextBox.Clear()
            tagsTextBox.Clear()
        End If

        createButton.Text = "Create Post"
        cancelButton.Enabled = True
        createButton.Enabled = True
    End Sub

    Private Sub cancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelButton.Click
        If MsgBox("Cancel editing this post? All unpublished changes will be lost.", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            Me.Close()
            titleTextBox.Clear()
            postTextBox.Clear()
            tagsTextBox.Clear()
        End If
    End Sub

End Class